using MediatR;
using Medical.Api.Write.Application.Command.BasicManagement.ShelfDictionaryCommand;
using Medical.Domain.BasicManagement;
using Medical.ErrorCode;
using Medical.Interstructrue.Base;

namespace Medical.Api.Write.Application.Handler.BasicManagement.ShelfDictionaryHandler
{
    /// <summary>
    /// 货架状态修改处理器
    /// </summary>
    public class UpdateShelvesStatusHandler : IRequestHandler<UpdateShelvesStatusCommand, ApiResult<bool>>
    {
        private readonly IBaseRepository<Shelves> _shelvesRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="shelvesRepository">货架仓储接口</param>
        public UpdateShelvesStatusHandler(IBaseRepository<Shelves> shelvesRepository)
        {
            _shelvesRepository = shelvesRepository;
        }

        /// <summary>
        /// 处理货架状态修改请求
        /// </summary>
        public Task<ApiResult<bool>> Handle(UpdateShelvesStatusCommand request, CancellationToken cancellationToken)
        {
            var shelves = _shelvesRepository.GetModel(request.StatusDto.Id);
            if (shelves == null)
            {
                return Task.FromResult(new ApiResult<bool>
                {
                    Code = ApiEnums.NotFound,
                    Msg = "未找到指定的货架",
                    Data = false
                });
            }

            shelves.Status = request.StatusDto.Status;
            _shelvesRepository.Update(shelves);

            return Task.FromResult(new ApiResult<bool>
            {
                Code = ApiEnums.Success,
                Msg = "货架状态修改成功",
                Data = true
            });
        }
    }
}